// index.js
import {
  get,
  post
} from '../../utils/request'
import {
  APIS
} from '../../utils/api'
import {
  timestampToTime
} from '../../utils/util'
// 获取应用实例
const app = getApp()
const statusMap = ['custom', 'today', 'yestoday', 'week', 'month'];
Page({
  data: {
    date: '',
    startDate: '-',
    endDate: '-',
    rangeStart: '2024-01-01',
    rangeEnd: '2099-01-01',
    currentIndex: 0,
    tabs: ['今日', '昨日', '本周', '本月'],
    operateList: [{
      text: '充值管理',
      bg: '#FFF9DF',
      url: '/pages/recharge/index?type=recharge',
    }, {
      text: '优惠券配置',
      bg: '#F2FFE2',
      url: '/pages/recharge/index?type=discount',
    }, {
      text: '经营管理',
      bg: '#FFEEDF',
      url: '/pages/management/index',
    }, {
      text: '台费配置',
      bg: '#E2FFF5',
      url: '/pages/table/index',
    }, {
      text: '门店管理',
      bg: '#FFDFF2',
      url: '/pages/storemanage/index',
    }, {
      text: '会员管理',
      bg: '#E2E4FF',
      url: '/pages/vip/index',
    }, {
      text: '赛事配置',
      bg: '#FFDFDF'
    }],
    shopInfo: {},
    amount: {}
  },
  onLoad() {
    const temp = new Date().getTime();
    const startDate = timestampToTime(temp - 24 * 60 * 60 * 1000);
    const endDate = timestampToTime(temp);
    this.setData({
      startDate,
      endDate,
      rangeEnd: endDate
    });
  },
  onShow() {
    const shopInfo = wx.getStorageSync('currShop');
    this.setData({
      shopInfo
    }, () => {
      this.getTotal(1);
    });
  },

  getTotal(idx, start, end) {
    post(APIS.statistics, {
        range: statusMap[idx],
        shop: this.data.shopInfo.id,
        start,
        end
      })
      .then(res => {
        let data = res.data;
        console.log(data);
        this.setData({
          amount: data,
          startDate: data.showDate.startDate,
          endDate: data.showDate.endDate,
        })
      })
  },

  handleJump(e) {
    const {
      item
    } = e.target.dataset;
    if (item && item.url) {
      let params = '';
      if (item.text === '经营管理') {
        let {
          startDate,
          endDate,
          currentIndex
        } = this.data;
        params = `?rangeType=${currentIndex + 1}&start=${startDate}&end=${endDate}`;
      }
      wx.navigateTo({
        url: `${item.url}${params}`
      })
    }
  },
  handleChange(e) {
    const {
      index
    } = e.target.dataset;
    this.setData({
      currentIndex: index,
    });

    this.getTotal(index + 1);
  },
  bindStartChange(e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    let date = e.detail.value;
    let endDate = new Date(date) < new Date(this.data.endDate) ?
      this.data.endDate :
      date
    this.setData({
      startDate: date,
      endDate,
      currentIndex: -1
    });

    this.getTotal(0, date, endDate)
  },
  bindEndChange(e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      endDate: e.detail.value,
      currentIndex: -1
    })

    this.getTotal(0, this.data.startDate, e.detail.value)
  },
  onShareAppMessage() {
    return getApp().globalData.shareMessage
  }
})